package com.sctvcloud.renhe.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.sctvcloud.renhe.beans.FocusColumn;
import com.sctvcloud.renhe.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.renhe.ui.fragment.TvFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19 0019.
 * 首页 管理各个栏目的页面适配器
 */

public class TvParentPagerAdapter extends FragmentStatePagerAdapter {



    private List<FocusColumn> list;
    private TvFragment tv;
//    private FocusColumn specail;
//    private FocusColumn anchor;
    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    private int topHeight;

    public TvParentPagerAdapter(FragmentManager fm, List<FocusColumn> list) {
        super(fm);
        this.list = list;

        fragmentWeaks = new SparseArray<>();
    }

    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        fragment = new TvFragment();
        fragment.setChannelUrl(list.get(position).getChannelUrl());
        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(fragment));
        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getChannelName();
    }

    public void setData(List<FocusColumn> data) {
        this.list = data;
//        for (int n = 0; n < list.size(); n++) {
//            if (list.get(n).getChannelType().equalsIgnoreCase("" + TYPE_SPECIAL)) {
//                specail = list.get(n);
//            } else if (list.get(n).getChannelType().equalsIgnoreCase("" + TYPE_ANCHOR)) {
//                anchor = list.get(n);
//                list.remove(n);
//            }
//        }
        notifyDataSetChanged();
    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }

    public int getPosCurrentPage(String type) {
        if (getCount() == 0) {
            return -1;
        }
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getChannelType().equals(type)) {
                return i;
            }
        }
        return -1;
    }

    public void setTopHeight(int topHeight) {
        this.topHeight = topHeight;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }


}
